<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>[[${application.sysName}]] | 登录</title>

    <link th:href="@{'/res/inspinia/css/bootstrap.min.css'}" rel="stylesheet">
    <link th:href="@{'/res/inspinia/font-awesome/css/font-awesome.css'}" rel="stylesheet">

    <!-- Toastr style -->
    <link th:href="@{'/res/inspinia/css/plugins/toastr/toastr.min.css'}" rel="stylesheet">

    <link th:href="@{'/res/inspinia/css/animate.css'}" rel="stylesheet">
    <link th:href="@{'/res/inspinia/css/style.css'}" rel="stylesheet">
<style type="text/css">
    .img-bg {
        background: #f3f3f4 url('/res/myimg/login-bg.png') no-repeat fixed top;
        background-size: cover;
    }

    .login-form {
        background-color: white;
    }
</style>
</head>

<body class="gray-bg img-bg">

<div class="middle-box text-center loginscreen animated fadeInDown card" style="margin-top: 40px;width: 800px;border-radius: .5rem;">
    <div class="card-body">
        <div>
            <h1 class="logo-name"><i class="fa fa-university"></i></h1>
        </div>
        <h3>欢迎登录新一代财务协同办公系统V2.0</h3>
        <p>made with love for a better work</p>
        <form class="m-t" role="form" id="loginForm" action="/login" method="post">
            <input type="hidden" id="uuid" name="uuid"/>
            <!--            <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>-->
            <div class="form-group">
                <input type="text" name="username" class="form-control" placeholder="用户名" required=""
                       data-validation="length alphanumeric" data-validation-length="min4">
            </div>
            <div class="form-group">
                <input type="password" name="password" class="form-control" placeholder="密码" required="">
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-4"><input type="text" name="captcha" maxlength="4" class="form-control"
                                              placeholder="验证码" required=""></div>
                    <div class="col-8"><img id="captchaImg" width="100%" src="" onclick="getCaptcha()"/></div>
                </div>
            </div>
            <button type="submit" class="btn btn-primary block full-width m-b">登录</button>
        </form>
        <p class="m-t">
            <small><strong>Copyright</strong> 河南峰炜信息技术有限公司 &copy; 2018-2020</small>
        </p>
    </div>
</div>

<!-- Mainly scripts -->
<script th:src="@{'/res/inspinia/js/jquery-3.1.1.min.js'}"></script>
<script th:src="@{'/res/inspinia/js/popper.min.js'}"></script>
<script th:src="@{'/res/inspinia/js/bootstrap.js'}"></script>

<!-- Toastr script -->
<script th:src="@{'/res/inspinia/js/plugins/toastr/toastr.min.js'}"></script>

<script th:src="@{'/res/inspinia/js/plugins/validate/jquery.validate.min.js'}"></script>
<script th:src="@{'/res/js/messages_zh.js'}"></script>
<!-- js-cookie -->
<script th:src="@{'/res/js/js.cookie.min.js'}"></script>
<!--自定义HTTP请求-->
<script th:src="@{'/res/myjs/httpRequest.js'}"></script>
<script type="text/javascript">
    $(function () {
        myAjaxSetup(1);
        getCaptcha()
        toastr.options = {
            version: '1',
            closeButton: false,
            debug: false,
            progressBar: true,
            preventDuplicates: false,
            positionClass: 'toast-top-right',
            onclick: null
        };
        $('#loginForm').validate({
            rules: {
                username: {required: true},
                password: {required: true}
            },
            submitHandler: function (form) {
                let formVals = $(form).serializeArray();
                let formData = {}
                for (let i = 0; i < formVals.length; i++) {
                    formData[formVals[i].name] = formVals[i].value;
                }
                toLogin(formData)
                if ($('input[name="username"]').val() != 'admin') {
                    // 登录网销
                    $.post({
                        url: '/portal/teacher/teacherLogin.json',
                        dataType: 'json',
                        data: {workNo: $('input[name="username"]').val()},
                        success: function (data) {
                            if (data.success) {

                            } else {
                                console.log("网销V2登录失败", data.msg);
                            }
                        }
                    });
                }
            }
        })
    })

    function toLogin(formData) {
        $.post({
            url: '/sys/login',
            dataType: 'json',
            data: formData,
            success: function (data) {
                if (data && data.code === 0) {
                    toastr.success("即将跳转到主页面", '登录成功')
                    $.get({
                        url: `/sys/menu/nav`,
                        dataType: 'json',
                        success: function (data) {
                            if (data && data.code === 0) {
                                localStorage.setItem("menuList", JSON.stringify(data.menuList));
                                window.location.href = "/";
                            } else {
                                toastr.error(data.msg, '操作失败')
                            }
                        }
                    })
                } else {
                    getCaptcha();
                    toastr.error(data.msg, '登录失败')
                }
            }
        });
    }

    // 获取验证码
    function getCaptcha() {
        let uuid = getUUID();
        $('#uuid').val(uuid);
        $('#captchaImg').attr('src', 'captcha.jpg?uuid=' + uuid);
    }

    function getUUID() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
            return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
        })
    }

    /**
     * 更新AJAX配置
     * @param tenantId
     */
    function myAjaxSetup(tenantId) {
        $.ajaxSetup({
            headers: {
                tenantId: tenantId
            },
            global: false,
            error: function (jqXHR, textStatus, errorThrown) {
                console.log("统一异常处理")
                console.log("jqXHR",jqXHR)
                console.log("textStatus",textStatus)
                console.log("errorThrown",errorThrown)
            }
        });
    }
</script>
</body>

</html>
